package cn.jwinni.springboot.controller;

import cn.jwinni.springboot.common.JsonResult;
import cn.jwinni.springboot.domain.Manage;
import cn.jwinni.springboot.service.ManageService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class ManageController {

    public static Logger logs = Logger.getLogger(ManageController.class);
    @Autowired
    private ManageService manageService;

    /**
     * 登录页
     *
     * @return
     */
    @RequestMapping("/")
    public String getRoot() {

        return "login";
    }

    /**
     * 登录页
     *
     * @return
     */
    @RequestMapping("/login")
    public String getLogin() {

        return "login";
    }

    @RequestMapping("/manageEditUI")
    public String getManageEditUI() {

        return "userInfo/manage_edit";
    }

    /**
     * 登录
     *
     * @param req
     * @param loginName
     * @param pwd
     * @return
     */
    @RequestMapping("/managelogin")
    @ResponseBody
    public JsonResult Login(HttpServletRequest req, String loginName, String pwd) {

        try {
            Manage manage = manageService.manageLogin(loginName, pwd);

            if (manage == null) {
                return new JsonResult("账号不存在");
            }
            if (manage.getPassword() != null && pwd.equals(manage.getPassword())) {

                HttpSession session = req.getSession();
                session.setAttribute("userToken", manage.getAdminName());
                session.setMaxInactiveInterval(30 * 60);
                return new JsonResult();
            } else {

                return new JsonResult("密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logs.error("function:Login，msg=" +e.getMessage());
            return new JsonResult("系统错误");
        }

    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping("/SignOut")
    public String SignOut(HttpServletRequest req){

        req.getSession().removeAttribute("userToken");
        return "login";
    }

    /**
     * 系统用户列表页面
     *
     * @return
     */
    @RequestMapping("/manageListUI")
    public String manageUI() {
        return "userInfo/manage_list";
    }

    /**
     * 系统管理员列表
     *
     * @param telNo
     * @param pageCurrent
     * @return
     */
    @ResponseBody
    @RequestMapping("/getManageList")
    public JsonResult getManageList(String telNo, Integer pageCurrent) {

        try {
            Map<String, Object> users = manageService.selectManageList(telNo, pageCurrent);
            return new JsonResult(users);
        } catch (Exception e) {
            e.printStackTrace();
            logs.error("function:getManageList，msg=" +e.getMessage());
            return new JsonResult("系统错误");
        }
    }

    /**
     * 新增管理员
     *
     * @param manageName
     * @param adminName
     * @param pwd
     * @param telNo
     * @return
     */
    @RequestMapping("/InsertManege")
    @ResponseBody
    public JsonResult insertManage(String manageName, String adminName, String pwd, String telNo) {
        try {
            Manage manage = new Manage();
            manage.setManageName(manageName);
            manage.setAdminName(adminName);
            manage.setPassword(pwd);
            manage.setTelNo(telNo);
            manageService.insertManage(manage);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            logs.error("function:insertManage，msg=" +e.getMessage());
            return new JsonResult("系统错误");
        }
    }

    /**
     * 修改管理员信息
     *
     * @param id
     * @param manageName
     * @param adminName
     * @param pwd
     * @param telNo
     * @return
     */
    @RequestMapping("/UpdateManage")
    @ResponseBody
    public JsonResult updateManage(Integer id, String manageName, String adminName, String pwd, String telNo) {
        try {
            Manage manage = new Manage();
            manage.setId(id);
            manage.setManageName(manageName);
            manage.setAdminName(adminName);
            manage.setPassword(pwd);
            manage.setTelNo(telNo);
            manageService.updateManage(manage);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            logs.error("function:updateManage，msg=" +e.getMessage());
            return new JsonResult(2, "系统错误");
        }
    }

    /**
     * 删除管理员
     *
     * @param checkedIds
     * @return
     */
    @RequestMapping("/DeleteManage")
    @ResponseBody
    public JsonResult DeleteManage(String checkedIds) {
        try {
            manageService.deleteManage(checkedIds);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            logs.error("function:DeleteManage，msg=" +e.getMessage());
            return new JsonResult("系统错误");
        }
    }


    /**
     * 通过id查询项目
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/getManageById")
    public JsonResult getManageById(Integer id) {
        Manage manage = null;
        try {
            manage = manageService.getManageById(id);
            return new JsonResult(manage);
        } catch (Exception e) {
            e.printStackTrace();
            logs.error("function:getManageById，msg=" +e.getMessage());
            return new JsonResult("系统错误");
        }
    }
}
